package edu.xmut.kakamusic.fragment.home;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.navigation.Navigation;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.adapter.BannerAdapter;
import com.youth.banner.transformer.ZoomOutPageTransformer;

import java.util.List;

import edu.xmut.kakamusic.R;
import edu.xmut.kakamusic.adapter.HomeAdapter;
import edu.xmut.kakamusic.adapter.ImageAdapter;
import edu.xmut.kakamusic.adapter.ImageTitleNumAdapter;
import edu.xmut.kakamusic.utils.ToastUtils;
import edu.xmut.kakamusic.viewmodel.HomeViewModel;


public class HomeFragment extends Fragment {


    private HomeViewModel homeViewModel;
    private HomeAdapter homeAdapter;
    private Banner<?, BannerAdapter<?, ?>> banner;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // 创建viewmodel
        homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
        // 实例化布局
        View root = inflater.inflate(R.layout.fragment_home, container, false);
        RecyclerView homeRecyclerView = root.findViewById(R.id.home_recyclerView);
        homeRecyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        // 设置多布局主页适配器
        homeAdapter = new HomeAdapter(null);
        homeRecyclerView.setAdapter(homeAdapter);
        // 设置空布局
        homeAdapter.setEmptyView(R.layout.empty_home);
        // 得到头部布局
        View headerView = inflater.inflate(R.layout.header_home, container, false);
        // 往多布局中添加头部
        homeAdapter.setHeaderView(headerView);
        // 设置头部是否为空
        homeAdapter.setHeaderWithEmptyEnable(true);
        // 得到banner
        banner = headerView.findViewById(R.id.banner_home);
        // 设置banner
        banner.addBannerLifecycleObserver(this)
                .setPageTransformer(new ZoomOutPageTransformer())
                .start();
        List<Integer> imageList = List.of(R.drawable.pic_item_list_default, R.drawable.pic_item_list_default, R.drawable.pic_item_list_default, R.drawable.pic_item_list_default);
        // 添加banner图片
        banner.setAdapter(new ImageAdapter(imageList));


        RefreshLayout refreshLayout = root.findViewById(R.id.homeRefreshLayout);
        refreshLayout.setOnRefreshListener(layout -> {
            layout.finishRefresh(2000);
            getAdList();
            getNewsList();
        });
        refreshLayout.setOnLoadMoreListener(layout -> {
            layout.finishLoadMore(2000);
            ToastUtils.show(requireContext(), "没有更多数据了...");
        });


        LinearLayout linearLayoutPython = headerView.findViewById(R.id.linearLayout_python);
        linearLayoutPython.setOnClickListener(v -> {
            Navigation.findNavController(v)
                    .navigate(R.id.action_menu_home_to_pythonFragment);
        });

        homeAdapter.setOnItemClickListener((adapter, view, position) -> {
            Bundle bundle = new Bundle();
            bundle.putString("url", homeAdapter.getData().get(position).getNewsUrl());
            Navigation.findNavController(view)
                    .navigate(R.id.action_menu_home_to_webFragment, bundle);
        });

        return root;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        getAdList();
        getNewsList();
    }


    private void getNewsList() {
        homeViewModel.getNewsList().observe(getViewLifecycleOwner(), news -> {
            homeAdapter.setList(news);
        });
    }

    private void getAdList() {
        homeViewModel.getAdList().observe(getViewLifecycleOwner(), news -> {
            banner.setAdapter(new ImageTitleNumAdapter(news));
        });
    }


}